package controller.session.login;

import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import controller.IndexController;
import controller.CatalogoController;
import dao.DAOUsuario;
import dao.entity.Usuario;

@Resource
public class LoginController {

    private Result result;
    private UserSession userSession;

    public LoginController(Result result, UserSession userSession) {
        this.result = result;
        this.userSession = userSession;
    }
    
    public void login() {
    	result.redirectTo(IndexController.class).index();
    }

    public void autenticar(Usuario usuario) {
        Usuario user = new DAOUsuario().autenticar(usuario);
        if (user != null) {
            userSession.setUser(user);
            result.redirectTo(CatalogoController.class).lista();
        } else {
            result.include("error", "E-mail ou senha incorreta!").redirectTo(this).login();
        }
    }

    public void logout() {
        userSession.logout();
        result.redirectTo(this).login();
    }

}